#include "weather_gui.h"
#include "weather_image.h"

#include "lv_port_indev.h"
#include "lvgl.h"


lv_obj_t *wc_scr[4];

lv_obj_t *weather_image = NULL;
lv_obj_t *cityname_label = NULL;
lv_obj_t *temperature_label = NULL;

lv_obj_t *time_image = NULL;
lv_obj_t *date_label = NULL;
lv_obj_t *time_label = NULL;


lv_obj_t  * calendar = NULL;
// static lv_group_t *g;
static lv_style_t default_style;
static lv_style_t label_style1;
static lv_style_t label_style2;
static lv_style_t label_style3;
static lv_style_t label_style4;


lv_font_declare(lv_font_montserrat_20);
lv_font_declare(lv_font_montserrat_24);
lv_font_declare(lv_font_montserrat_40);

// 天气图标路径的映射关系
const void *image_map1[] = {&qika,&yitou,&remen,&mi,&sipi,&wutu,&liuk};
int map_index1[] = {0,1,2,3,4,5,6};


void weather_obj_del(void);

void weather_gui_init(void)
{
    lv_style_init(&default_style);
    lv_style_set_bg_color(&default_style, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_style_set_bg_color(&default_style, LV_STATE_PRESSED, LV_COLOR_GRAY);
    lv_style_set_bg_color(&default_style, LV_STATE_FOCUSED, LV_COLOR_BLACK);
    lv_style_set_bg_color(&default_style, LV_STATE_FOCUSED | LV_STATE_PRESSED, lv_color_hex(0xf88));

    // 天气页初始化
    wc_scr[0] = lv_obj_create(NULL, NULL);
    lv_obj_add_style(wc_scr[0], LV_BTN_PART_MAIN, &default_style);
    lv_style_init(&label_style1);
    lv_style_set_text_opa(&label_style1, LV_STATE_DEFAULT, LV_OPA_COVER);
    lv_style_set_text_color(&label_style1, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_style_set_text_font(&label_style1, LV_STATE_DEFAULT, &lv_font_montserrat_24);
    lv_style_init(&label_style2);
    lv_style_set_text_opa(&label_style2, LV_STATE_DEFAULT, LV_OPA_COVER);
    lv_style_set_text_color(&label_style2, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_style_set_text_font(&label_style2, LV_STATE_DEFAULT, &lv_font_montserrat_40);

    // 日期时间页初始化
    wc_scr[1] = lv_obj_create(NULL, NULL);
    lv_obj_add_style(wc_scr[1], LV_BTN_PART_MAIN, &default_style);
    lv_style_init(&label_style3);
    lv_style_set_text_opa(&label_style3, LV_STATE_DEFAULT, LV_OPA_COVER);
    lv_style_set_text_color(&label_style3, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_style_set_text_font(&label_style3, LV_STATE_DEFAULT, &lv_font_montserrat_40);

    // 硬件信息页初始化
    wc_scr[2] = lv_obj_create(NULL, NULL);
    lv_obj_add_style(wc_scr[2], LV_BTN_PART_MAIN, &default_style);
    lv_style_init(&label_style4);
    lv_style_set_text_opa(&label_style4, LV_STATE_DEFAULT, LV_OPA_COVER);
    lv_style_set_text_color(&label_style4, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_style_set_text_font(&label_style4, LV_STATE_DEFAULT, &lv_font_montserrat_20);
}

void display_weather_init()
{
    lv_obj_t *act_obj = lv_scr_act(); // 获取当前活动页
    if (act_obj == wc_scr[0])
        return;
    weather_obj_del();
    lv_obj_clean(act_obj); // 清空此前页面
    weather_image = lv_img_create(wc_scr[0], NULL);
    cityname_label = lv_label_create(wc_scr[0], NULL);
    temperature_label = lv_label_create(wc_scr[0], NULL);
}

void display_weather(const char *cityname, const char *temperature, int weathercode, lv_scr_load_anim_t anim_type)
{
    display_weather_init();
    const void *path = image_map1[map_index1[weathercode]];
   
    lv_img_set_src(weather_image, path);
    lv_obj_align(weather_image, NULL, LV_ALIGN_OUT_TOP_MID, 0, 140);

    lv_obj_add_style(cityname_label, LV_LABEL_PART_MAIN, &label_style2);
    lv_label_set_text(cityname_label, cityname);
    lv_obj_align(cityname_label, NULL, LV_ALIGN_OUT_BOTTOM_LEFT, 15, -95);

    lv_obj_add_style(temperature_label, LV_LABEL_PART_MAIN, &label_style2);
    lv_label_set_text(temperature_label ,temperature);
    lv_obj_align(temperature_label, NULL, LV_ALIGN_OUT_BOTTOM_LEFT, 15, -50);

    lv_scr_load_anim(wc_scr[0], anim_type, 300, 300, false);
}

void display_time_init()
{
    lv_obj_t *act_obj = lv_scr_act(); // 获取当前活动页
    if (act_obj == wc_scr[1])
        return;
    weather_obj_del();
    lv_obj_clean(act_obj); // 清空此前页面
    time_image = lv_img_create(wc_scr[1], NULL);
    date_label = lv_label_create(wc_scr[1], NULL);
    time_label = lv_label_create(wc_scr[1], NULL);
}


void display_time(const char *date, const char *time, lv_scr_load_anim_t anim_type)
{
    display_time_init();

    lv_img_set_src(time_image, &tian);
    lv_obj_align(time_image, NULL, LV_ALIGN_OUT_TOP_MID, 0, 110);

    lv_obj_add_style(date_label, LV_LABEL_PART_MAIN, &label_style3);
  
    lv_label_set_text_fmt(date_label, "%s°C", date);
    lv_obj_set_pos(date_label, 0,0);

    lv_obj_add_style(time_label, LV_LABEL_PART_MAIN, &label_style3);
    lv_label_set_text(time_label, time);
    lv_obj_set_pos(time_label, 120,60);

    lv_scr_load_anim(wc_scr[1], anim_type, 300, 300, false);
}

void display_hardware_init()
{
    lv_obj_t *act_obj = lv_scr_act(); // 获取当前活动页
    if (act_obj == wc_scr[2])
        return;
    weather_obj_del();
    lv_obj_clean(act_obj); // 清空此前页面
    calendar = lv_calendar_create(wc_scr[2], NULL);
  
}
void display_hardware(int yer,int mon , int da, lv_scr_load_anim_t anim_type)
{
    display_hardware_init();
    lv_obj_set_size(calendar, 240, 240);
    lv_obj_align(calendar, NULL, LV_ALIGN_CENTER, 0, 0);  
    lv_obj_set_style_local_text_font(calendar, LV_CALENDAR_PART_DATE, LV_STATE_DEFAULT, lv_theme_get_font_small());




            lv_calendar_date_t today;
            today.year = yer;
            today.month = mon;
            today.day = da;

            lv_calendar_set_today_date(calendar, &today);
            lv_calendar_set_showed_date(calendar, &today);


    lv_scr_load_anim(wc_scr[2], anim_type, 300, 300, false);
}

void weather_obj_del(void)
{
    if (NULL != weather_image)
    {
        lv_obj_clean(weather_image);
        lv_obj_clean(cityname_label);
        lv_obj_clean(temperature_label);
        weather_image = NULL;
        cityname_label = NULL;
        temperature_label = NULL;
    }

    if (NULL != time_image)
    {
        lv_obj_clean(time_image);
        lv_obj_clean(date_label);
        lv_obj_clean(time_label);
        time_image = NULL;
        date_label = NULL;
        time_label = NULL;
    }

    if (NULL != calendar)
    {
        lv_obj_clean(calendar);
       
        calendar = NULL;
       
    }
}

void weather_gui_del(void)
{
    weather_obj_del();

    for(int pos=0; pos<4; ++pos)
    {
        if(NULL != wc_scr[0])
        {
            lv_obj_clean(wc_scr[0]); // 清空此前页面
            wc_scr[0] = NULL;
        }
    }
}